import mysql from "mysql";

export default class sql {
    db;
    constructor() {
        this.db = mysql.createConnection({
            host: "localhost",
            port: 3306,
            user: "root",
            password: "root",
            database: "goods"
        })
    }
    isConnet() {
        return new Promise((resolve, reject) => {
            this.db.connect(function (err) {
                if (err) {
                    resolve(false);
                } else {
                    resolve(true);
                }
            })
        })
    }
    getAllData(arr,_this){
        return new Promise((resolve, reject)=> {
            var sqlStr = "SELECT * FROM `user` WHERE 1"
        
            _this.db.query(sqlStr, arr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    resolve(res);
                }
            })
        })
    }
    searchUserdata(arr,_this){
       
        return new Promise((resolve, reject)=> {
            var sqlStr = "SELECT  * FROM `car` WHERE user"+ "="+ arr;
        
            _this.db.query(sqlStr, arr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                   
                    resolve(res);
                }
            })
        })
    }
    searchUser(arr,_this){
        return new Promise((resolve, reject)=> {
            var sqlStr = "SELECT `user`, `password` FROM `login` WHERE user"+ "="+ arr;
            console.log(sqlStr);
            _this.db.query(sqlStr, arr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    resolve(res);
                }
            })
        })
    }
    insertCar(arr,_this){
        return new Promise((resolve, reject)=> {
            var sqlStr = "INSERT INTO `car`(`user`, `productSid`, `num`,`size`,`color`)VALUES(?,?,?,?,?)"
            console.log(sqlStr);
            _this.db.query(sqlStr, arr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    resolve(res);
                }
            })
        })
    }
    updateCar(_this){
        return new Promise((resolve, reject)=> {
            var sqlStr = "SELECT `user`, `productSid`, `num` FROM `car` WHERE num";
            _this.db.query(sqlStr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    resolve(res);
                }
            })
        }) 
    }
    delData(id,_this){
        return new Promise((resolve, reject)=> {
            var sqlStr = "DELETE FROM `car` WHERE productSid="+id;
         console.log(sqlStr);
            _this.db.query(sqlStr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    resolve(true);
                }
            })
        })
    }
    getCar(arr,_this){ 
       
        return new Promise((resolve, reject)=> {
            var sqlStr = "UPDATE `car` SET `num`"+"="+arr.num +""+ " WHERE productSid" +"="+arr.productSid;
         console.log(sqlStr);
            _this.db.query(sqlStr, arr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    resolve(true);
                }
            })
        })
    }
    insertUser(arr,_this){
        return new Promise((resolve, reject)=> {
            var sqlStr = "INSERT INTO `login`(`user`,`password`)VALUES(?,?)"
            _this.db.query(sqlStr, arr, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    resolve(true);
                }
            })
        })
    }
    insertDB(arr,_this) {
        var arr1=[]
        for (var i in arr){  
           arr1.push(arr[i])
        }
        return new Promise((resolve, reject)=> {
            var sqlStr = "INSERT INTO `user`( `productSid`, `brand`, `minMarkOffPrice`, `minSellingPrice`, `proPictDir`, `skuList`, `classification`, `productName`, `brandName`, `store`, `detail`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
            _this.db.query(sqlStr, arr1, function (err, res) {     
                if (err) {
                    resolve(false);
                } else {
                    
                    resolve(true);
                }
            })
        })
    }
     selectItem(data,_this,n){
        return new Promise(function(resolve,reject){
           var str="SELECT `pid`, `productSid`, `brand`, `minMarkOffPrice`, `minSellingPrice`, `proPictDir`, `skuList`, `classification`, `productName`, `brandName`, `store`, `detail` FROM `user` WHERE "+n +"=" +  data;
           _this.db.query(str,function(err,res){
            if (err) {
                resolve(false);
            } else {
                
                resolve(res);
            }
            })
        })
    }
    

}

//  module.exports=sql;
// exports.sql=sql